﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Entity;

namespace T.FAS.Runtime.Action.ExpressionPlugin
{
    public class CommonExpProvider: BaseProvider
    {
        private static readonly object lockObj = new object();
        public static CommonExpProvider GetInstance()
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        instance = new CommonExpProvider();
                    }
                }
            }
            return instance;
        }
        static CommonExpProvider instance = null;
        ExpressionPluginManager manager = null;
        private CommonExpProvider()
        {
            manager = new ExpressionPluginManager();
            manager.provider = this;
        }

        public TRecord BuildTResultSchema(ExprItem item)
        {
            var result = new TRecord();
            var schema = manager.UpdateExprItemChildren(item);
            var propertiesDic = new Dictionary<string, object>();
            propertiesDic.Add("ActionID", schema.ActionID);
            propertiesDic.Add("ActionCode", schema.ActionCode);
            propertiesDic.Add("ActionName", schema.ActionName);
            if (item.Children != null && item.Children.Count > 0)
            {
                var countItems = item.Children.FindAll(x=>x.Code.Equals("count",StringComparison.OrdinalIgnoreCase));
                var cont = string.Join(",",item.Children.Select(x=>x.ExprMetaDataAlias+ x.Code));
                item.Children.ForEach(expr =>
                {
                    if (propertiesDic.ContainsKey(expr.Code))
                    {
                        throw new FasException("ActionDevExpressionPluginError1001", $"已经包含字段{expr.Code},请检查元数据字段设置");
                    }
                    if (expr.IsLeaf)
                    {
                        propertiesDic.Add(expr.Code, "");
                    }
                    else
                    {
                        //propertiesDic.Add(TUtil.GetPropertyName(expr.Code,expr.ValueType == DataType.List), BuildRecordSchemaByExprItem(expr.Children));
                        propertiesDic.Add(expr.Code, BuildRecordSchemaByExprItem(expr.Children));
                    }
                });
            }
            result = new TRecord(propertiesDic);
            return result;
        }

        private TRecord BuildRecordSchemaByExprItem(List<ExprItem> items)
        {
            var propertiesDic = new Dictionary<string, object>();
            foreach (var item in items)
            {
                if (item.IsLeaf)
                {
                    propertiesDic.Add(item.Code, "");
                }
                else
                {
                    if (item.Children != null)
                    {
                        var subPropertiesDic = new Dictionary<string, object>();
                        foreach (var expr in item.Children)
                        {
                            if (expr.IsLeaf)
                            {
                                subPropertiesDic.Add(expr.Code, "");
                            }
                            else
                            {
                                var childRecord = BuildRecordSchemaByExprItem(expr.Children);
                                subPropertiesDic.Add(expr.Code, childRecord);
                            }
                        }
                        var subRecord = new TRecord(subPropertiesDic);
                        propertiesDic.Add(item.Code, subRecord);
                    }
                    else
                    {
                        propertiesDic.Add(item.Code, "");
                    }
                }
            }
            return new TRecord(propertiesDic);
        }
    }
}
